package com.gxa.day2022_08_09_28.emp_dept_system.dao.impl;


import com.gxa.day2022_08_09_28.emp_dept_system.dao.DeptDao;
import com.gxa.day2022_08_09_28.emp_dept_system.entity.Dept;
import com.gxa.day2022_08_09_28.emp_dept_system.utils.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class DeptDaoImpl implements DeptDao {
    @Override
    public Vector<Dept> queryAll() {
        Vector<Dept> depts = new Vector<>();
        Connection connection = DBConnection.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "SELECT * FROM dept";
        try {
             ps = connection.prepareStatement(sql);
             rs = ps.executeQuery();

            while(rs.next()) {
                 int deptno = rs.getInt("deptno");
                 String dname = rs.getString("dname");
                 String loc = rs.getString("loc");
                 Dept dept = new Dept(deptno,dname,loc);

                 depts.add(dept);

             }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBConnection.close(rs,ps,connection);
        }

        return depts;
    }
}
